﻿using System;

/*代表食物*/
public class Food
{
	//食物的位置，食物生成区域的宽度与高度
	public int x { get; private set; }
	public int y { get; private set; }
	public char element { get; private set; } = '8';
	public PixelContentType type { get; private set; } = PixelContentType.Food;
	private int screenWidth;
	private int screenHeight;
	public bool exist = false;

	public Food(int width, int height, char element)
	{
		this.screenHeight = height;
		this.screenWidth = width;
		this.element = element;
	}

	//在所有空白像素中随机选择一个，并更改food的位置
	public void SetPosition(PixelContent[,] pixels)
    {
		int randomSeed = (int)DateTime.Now.Ticks;
		Random rand = new Random(randomSeed);

		int screenWidth = pixels.GetLength(0);
		int screenHeight = pixels.GetLength(1);
        int blankPixelsNum = 0;
        int[,] blankPixels = new int[pixels.Length,2];
		int orderNumber = 0;

		for(int ix = 0; ix < screenWidth; ix++)
        {
			for(int iy = 0; iy < screenHeight; iy++)
            {
				if(pixels[ix,iy].contentType == PixelContentType.Blank)
                {
					blankPixelsNum++;
					blankPixels[blankPixelsNum - 1,0] = ix;
					blankPixels[blankPixelsNum - 1,1] = iy;
                }
				continue;
            }
        }

		orderNumber = rand.Next(0, blankPixelsNum - 1);
		x = blankPixels[orderNumber, 0];
		y = blankPixels[orderNumber, 1];

		this.exist = true;
    }
}
